<?php
/**
 * ------------------------------------------------------------------------
 * JA Toppanel module for Joomla 1.7.x
 * ------------------------------------------------------------------------
 * Copyright (C) 2004-2011 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
 * @license - Copyrighted Commercial Software
 * Author: J.O.O.M Solutions Co., Ltd
 * Websites:  http://www.joomlart.com -  http://www.joomlancers.com
 * This file may not be redistributed in whole or significant part.
 * ------------------------------------------------------------------------
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

require_once JPATH_SITE.'/components/com_content/helpers/route.php';
JModel::addIncludePath(JPATH_SITE.'/components/com_content/models');

class modJaToppanelHelper
{
	var $_result = null;
	var $_params = null;
	function _load($params)
	{		
		$this->_params = $params;
		switch ($params->get('type')){			
			case 'modules':				
				$this->rend_html_Modules();
				break;
			case 'module':
				$this->rend_html_Module();
				break;
			case 'article-ids':
			case 'article-catid':
				$this->rend_html_Article();
				break;	
		}
		
		return $this->_result;
	}
	
	function rend_html_Modules(){
		$lis = '';
		$list =  JModuleHelper::getModules(trim($this->_params->get('modules', '')));
		$mparams = array('style' => 'jaxhtml');
		$countlist = count($list) - 1;
		for($j=0; $j<= $countlist; $j++){
			$classitem = "";
			if($j == 0){
				$classitem = "first-item";
			}
			elseif($j == $countlist)
			{
				$classitem = "last-item";
			}
			if($list[$j]->module!='mod_jatoppanel'){
				$lis  .= "<li class=\"".$classitem."\" title=\"".strip_tags($list[$j]->title)."\">";
				//<h3><span>".$list[$j]->title."</span></h3>";
				$lis .= JModuleHelper::renderModule($list[$j], $mparams);	
				$lis .= '</li>';				
			}			
		}		
			
		if($lis!=''){			
			$class = $this->_params->get('display', 'ja-toppanel-col');
			$this->writeHTML($lis, $class);
		}		
	}
	
	function rend_html_Module(){
		$lis = ''; $list_module=array();
		if($this->_params->get('modulename', '')) $list_module = explode(",", $this->_params->get('modulename'));	
		for($i=0; $i<count($list_module); $i++){
			if ($list_module[$i]!='mod_jatoppanel') {
				$module =  JModuleHelper::getModule(substr(trim($list_module[$i]), 4 ));
				if($module && $module->id){
					$lis  .= "<li title=\"".strip_tags($module->title)."\"><h3><span>".$module->title."</span></h3>";
					$lis .= JModuleHelper::renderModule($module);	
					$lis .= '</li>';				
				}			
			}
				
		}		
		if($lis!=''){			
			$class = $this->_params->get('display', 'ja-toppanel-col');
			$this->writeHTML($lis, $class);
		}			
	}		
	
	function rend_html_Article(){
		$list = null; $lis = '';
		if($this->_params->get('type')=='article-ids' && $this->_params->get('article-ids', '')){
			$list = $this->getList($this->_params->get('article-ids', ''));
		}
		elseif ($this->_params->get('type') == 'article-catid'){
			$catids = (is_array($this->_params->get('article-catid', array())) && $this->_params->get('article-catid', array()))?implode(',', $this->_params->get('article-catid', '')):$this->_params->get('article-catid', '');			
			$list = $this->getList('', $catids, $this->_params->get('limit', -1));
		}				
				
		if($list){
			$introtext_limit = $this->_params->get('maxchars', 100);			
			foreach ($list as $row){
				
				$row->displayIntrotext = JHtml::_('string.truncate', JHtml::_('content.prepare', $row->introtext), '<p><a>', $introtext_limit);
				
				$lis  .= "<li title=\"".strip_tags($row->title)."\"><div class='ja-toppanel-article'><h3><span><a href='$row->link' title='{$row->title}'>{$row->title}</a></span></h3>";
				$lis .= $row->displayIntrotext;
				$lis .= '</div></li>';														
			}
			
			$class = $this->_params->get('display', 'ja-toppanel-col');
			$this->writeHTML($lis, $class);	
		}
		
		return '';
	}

	function writeHTML($content, $class=''){
		$this->_result = "<ul class='$class'>";
		$this->_result .= $content;
		$this->_result .= '</ul>';
		
	}
		
	
	public function getList($ids='', $catid='', $limit=0)
	{
		// Get the dbo
		$db = JFactory::getDbo();

		// Get an instance of the generic articles model
		$model = JModel::getInstance('Articles', 'ContentModel', array('ignore_request' => true));

		// Set application parameters in model
		$appParams = JFactory::getApplication()->getParams();
		$model->setState('params', $appParams);

		// Set the filters based on the module params
		$model->setState('list.start', 0);
		if(($catid != '' || $this->_params->get('type') == 'article-catid') && $limit>0) {
			$model->setState('list.limit', $limit);
		}
		
		$model->setState('filter.published', 1);

		// Access filter
		$access = !JComponentHelper::getParams('com_content')->get('show_noauth');
		$authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
		$model->setState('filter.access', $access);

		// Category filter
		if($catid){
			$model->setState('filter.category_id', explode(',', $catid));
		}
		if($ids){
			$model->setState('filter.article_id', explode(',', $ids));
		}
		
		// Set ordering
		$order_map = array(
			'm_dsc' => 'a.modified DESC, a.created',
			'mc_dsc' => 'CASE WHEN (a.modified = '.$db->quote($db->getNullDate()).') THEN a.created ELSE a.modified END',
			'c_dsc' => 'a.created',
			'p_dsc' => 'a.publish_up',
		);
		$ordering = JArrayHelper::getValue($order_map, 'a.ordering', 'a.publish_up');
		$dir = 'DESC';

		$model->setState('list.ordering', $ordering);
		$model->setState('list.direction', $dir);

		$items = $model->getItems();

		foreach ($items as &$item) {
			$item->slug = $item->id.':'.$item->alias;
			$item->catslug = $item->catid.':'.$item->category_alias;

			if ($access || in_array($item->access, $authorised))
			{
				// We know that user has the privilege to view the article
				$item->link = JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catslug));
			}
			else {
				$item->link = JRoute::_('index.php?option=com_user&view=login');
			}

			$item->introtext = JHtml::_('content.prepare', $item->introtext);
		}

		return $items;
	}
	function ae_detect_ie()
	{
		if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false))
			return true;
		else
			return false;
	}
}

?>
